/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.syr.iis.web.model.Assignment;
import edu.syr.iis.web.model.AssignmentManager;
import edu.syr.iis.web.model.ConversationLabelManager;
import edu.syr.iis.web.model.ConversationManager;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;
import edu.syr.iis.web.util.Key;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * @author kinoue
 *
 */
public class AssignmentListController  extends SimpleFormController 
{
    UserManager userManager;
    AssignmentManager assignmentManager;
    
	@Override
	protected Object formBackingObject(HttpServletRequest request) throws Exception 
	{
		User user = (User) request.getSession().getAttribute(Key.USER);
		
		List<Assignment> assignments = null;
		
		if (request.getParameter(Key.DATASET_ID) != null
				&& request.getParameter(Key.DATASET_ID).trim().length() > 0)
		{		
			int datasetId = Integer.parseInt(request.getParameter(Key.DATASET_ID));
			if (user.getRole() == User.ANNOTATOR)
			{
				assignments = assignmentManager.getAssignments(datasetId, user);
			} 
			else  // Accessed by admin
			{
				assignments = new ArrayList<Assignment>();
				for (User u: userManager.getUsers())
				{
					assignments.addAll(assignmentManager.getAssignments(datasetId, u));					
				}
			}
		}
		return new AssignmentsWrapper(assignments);
	}

	@Override
	protected Map<String,Object> referenceData(HttpServletRequest request) throws Exception
	{
		// For the future, I would like to add a drop down menu to select an annotator
		//
		HashMap<String, Object> model = new HashMap<String,Object>();
		List<User>users = userManager.getUsers();  
		model.put(Key.USERS, users);
		return model;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	
	public void setAssignmentManager(AssignmentManager assignmentManager) {
		this.assignmentManager = assignmentManager;
	}

	@Override
	protected ModelAndView onSubmit(
			HttpServletRequest request,
			HttpServletResponse response,
			Object command,
			BindException errors)
	{
		List<Assignment> assignments = ((AssignmentsWrapper) command).getAssignments();

		for (Assignment assignment: assignments)
			assignmentManager.saveAssignment(assignment);
		
		// Going back to the form
		String view = "redirect:"+ request.getRequestURL() + "?" + request.getQueryString();
		return new ModelAndView(view);
	}

	
	public class AssignmentsWrapper {
		List<Assignment> assignments;
		
		public AssignmentsWrapper(List<Assignment> assignments)
		{
			this.assignments = assignments;
		}
		
		public Assignment[] getAssignmentsArray()
		{
			if (assignments != null) 
				return assignments.toArray(new Assignment[assignments.size()]);
			else 
				return null;
		}
		public List<Assignment> getAssignments() {
			return assignments;
		}		
	}

}
